package com.umeox.moto.watch.fitness.data;

import static android.content.Context.SENSOR_SERVICE;
import static com.umeox.moto.common.log.Logger.getTime;
import static com.umeox.moto.watch.fitness.utils.ICache.KEY_STEP_COUNT_TOTAL;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.Log;

import com.umeox.moto.common.log.Logger;
import com.umeox.moto.watch.fitness.base.BaseApp;
import com.umeox.moto.watch.fitness.utils.CacheImpl;
import com.umeox.moto.watch.fitness.utils.ICache;
import com.umeox.moto.watch.fitness.utils.StepManager;


public class PedometerSensorData implements PedometerDataSource {

    private final PedometerContract.Presenter mPresenter;
    private final ICache mCache;
    private int mCacheStepCount;
    private SensorManager mSensorManager;
    private int mCurrentSteps;
    private static final String TAG = "PedometerSensorData";
    private long mTime;
    private int mLastStepCount;

    public PedometerSensorData(PedometerContract.Presenter presenter) {
        mPresenter = presenter;
        mCache = CacheImpl.getInstance();
        mCacheStepCount = mCache.get(KEY_STEP_COUNT_TOTAL, 0);
        mCurrentSteps = mCacheStepCount;
    }

    @Override
    public void load() {
        Logger.d(TAG, "load: " + mCacheStepCount);
        if (mLastStepCount != mCurrentSteps) {
            mLastStepCount = mCurrentSteps;
            mPresenter.onStepCountChange(mCurrentSteps);
        }

    }

    private SensorEventListener mSensorEventListener = new SensorEventListener() {
        @Override
        public void onSensorChanged(SensorEvent event) {
            Sensor sensor = event.sensor;
            int sensorSteps = (int) event.values[0];
            if (sensor.getType() == Sensor.TYPE_STEP_COUNTER) {
                Log.d("Pedometer", "onSensorChanged: " + sensorSteps + " cache:" + mCacheStepCount);
                mCurrentSteps = sensorSteps + mCacheStepCount;
                mTime = getTime(mTime);
                mPresenter.onStepCountChange(mCurrentSteps);
            }
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {

        }
    };


    @Override
    public void getStepInfo() {

    }


    @Override
    public void onCreate() {
        mSensorManager = (SensorManager) BaseApp.getAppContext().getSystemService(SENSOR_SERVICE);
        final boolean registered = mSensorManager.registerListener(mSensorEventListener, mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER), SensorManager.SENSOR_DELAY_NORMAL);
        Log.d(TAG, "onCreate: " + registered);
    }

    @Override
    public void onDestroy() {
        Log.d(TAG, "onDestroy: ");
        if (mSensorManager != null) {
            mSensorManager.unregisterListener(mSensorEventListener);
        }
    }

    @Override
    public void saveTotalStepCount() {
        Logger.e(TAG,"mCurrentSteps= " + mCurrentSteps);
        mCache.put(KEY_STEP_COUNT_TOTAL, mCurrentSteps);
        Logger.e(TAG,"mCurrentSteps from cache= " + mCache.get(KEY_STEP_COUNT_TOTAL,-1));
    }

    @Override
    public void resetAndSaveStepCount(int currentMillis, int saveTimeMillis) {
        Logger.e(TAG,"currentMillis="  +  currentMillis + ",saveTimeMillis=" + saveTimeMillis);
        ICache cache = CacheImpl.getInstance();
        StepManager stepManager = StepManager.getInstance();
        cache.put(ICache.KEY_STEP_TIME, currentMillis);
        stepManager.resetSensorFlag();
        cache.remove(KEY_STEP_COUNT_TOTAL);
        mCurrentSteps = 0;
        mCacheStepCount = 0;
    }
}
